package com.example.webpractice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.webpractice.annotation.IgnoreAuth;
import com.example.webpractice.dto.JiaoshiDTO;
import com.example.webpractice.dto.LoginDTO;
import com.example.webpractice.dto.UpdatePwdDTO;
import com.example.webpractice.po.JiaoshiPO;
import com.example.webpractice.service.JiaoshiService;
import com.example.webpractice.service.TokenService;
import com.example.webpractice.utils.ServerResponseEntity;
import com.example.webpractice.vo.JiaoshiVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

import static com.example.webpractice.utils.ResponseEnum.deleteFail;
import static com.example.webpractice.utils.ResponseEnum.updateFail;

/**
 * 教师
 * 后端接口
 */
@RestController
@RequestMapping("/jiaoshi")
public class JiaoshiController {
    @Autowired
    private JiaoshiService jiaoshiService;

    @Autowired
    private TokenService tokenService;

    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public ServerResponseEntity page(@RequestParam(name = "page") Integer page,
                                     @RequestParam(name = "limit") Integer limit,
                                     @RequestParam(name = "gonghao", required = false) String gonghao,
                                     @RequestParam(name = "jiaoshixingming", required = false) String jiaoshixingming) {
        QueryWrapper<JiaoshiPO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(jiaoshixingming)) {
            queryWrapper.like("jiaoshixingming", jiaoshixingming);
        }
        if (StringUtils.isNotBlank(gonghao)) {
            queryWrapper.like("gonghao", gonghao);
        }
        return ServerResponseEntity.success(jiaoshiService.queryPage(page, limit, queryWrapper));
    }


    /**
     * 详情
     */
    @RequestMapping("/info/{id}")
    public ServerResponseEntity info(@PathVariable("id") Long id) {
        JiaoshiVO jiaoshiVO = jiaoshiService.selectById(id);
        return ServerResponseEntity.success(jiaoshiVO);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public ServerResponseEntity save(@RequestBody JiaoshiDTO jiaoshiDto) {
        if (jiaoshiService.selectByGonghao(jiaoshiDto.getGonghao()) != null) {
            return ServerResponseEntity.fail(updateFail);
        }
        jiaoshiService.create(jiaoshiDto);
        return ServerResponseEntity.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public ServerResponseEntity update(@RequestBody JiaoshiDTO jiaoshiDto) {
        if (jiaoshiService.updateJiaoshi(jiaoshiDto) == 1) {
            return ServerResponseEntity.success();
        } else {
            return ServerResponseEntity.fail(updateFail);
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ServerResponseEntity delete(@RequestBody Long[] ids) {//todo 需要删除所有学生的相关信息
        if (jiaoshiService.deleteBatchIds(Arrays.asList(ids)) != ids.length) {
            return ServerResponseEntity.fail(deleteFail);
        }
        return ServerResponseEntity.success();
    }

    @IgnoreAuth
    @RequestMapping("/login")
    public ServerResponseEntity login(@RequestBody LoginDTO loginDTO) {
        JiaoshiPO po = jiaoshiService.selectByUsername(loginDTO.getUsername());
        if (po == null || !po.getMima().equals(loginDTO.getPassword())) {
            return ServerResponseEntity.fail(-1, "用户名或者密码错误");
        }
        String token = tokenService.generateToken(po.getId(), po.getGonghao(), "jiaoshi", po.getRole());
        return ServerResponseEntity.success(token);
    }

    @RequestMapping("/session")
    public ServerResponseEntity seesion(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        JiaoshiVO vo = jiaoshiService.selectById(userId);
        return ServerResponseEntity.success(vo);
    }

    @RequestMapping("/updatePasswd")
    public ServerResponseEntity updatePwd(@RequestBody UpdatePwdDTO updatePwdDTO) {
        JiaoshiPO po = jiaoshiService.selectByUsername(updatePwdDTO.getUsername());
        if (po == null || !po.getMima().equals(updatePwdDTO.getOldpasswd())) {
            return ServerResponseEntity.fail(-1, "用户名或者密码错误");
        }
        po.setMima(updatePwdDTO.getNewpasswd());
        if (jiaoshiService.updateById(po) != 1) {
            return ServerResponseEntity.fail(updateFail);
        }
        return ServerResponseEntity.success();
    }















/*
    @IgnoreAuth
    @RequestMapping("/login")
    public ServerResponseEntity login(@RequestBody LoginDTO loginDTO) {
        JiaoshiPO po = jiaoshiService.selectByUsername(loginDTO.getUsername());
        if (po == null || !po.getMima().equals(loginDTO.getPassword())) {
            return ServerResponseEntity.fail(-1, "用户名或者密码错误");
        }
        String token = tokenService.generateToken(po.getId(), po.getGonghao(), "jiaoshi", po.getRole());
        return ServerResponseEntity.success(token);
    }

    @RequestMapping("/session")
    public ServerResponseEntity seesion(HttpServletRequest request) {
        Long id = (Long) request.getAttribute("userId");
        JiaoshiVO jiaoshiVO = jiaoshiService.selectById(id);
        return ServerResponseEntity.success(jiaoshiVO);
    }

    @RequestMapping("updatePasswd")
    public ServerResponseEntity updatePwd(@RequestBody UpdatePwdDTO updatePwdDTO) {
        JiaoshiPO po = jiaoshiService.selectByUsername(updatePwdDTO.getUsername());
        if (po == null || !po.getMima().equals(updatePwdDTO.getOldpasswd())) {
            return ServerResponseEntity.fail(loginFail);
        }
        po.setMima(updatePwdDTO.getNewpasswd());
        if (jiaoshiService.updateById(po) != 1) {
            return ServerResponseEntity.fail(updateFail);
        }
        return ServerResponseEntity.success();
    }*/

}
